#!/bin/bash

# author:wangzk@unitechs.com
# date: 2019-05-28
# 修改记录
# 2019-06-03
# 1.对conf目录配置文件进行遍历拼装

# 检查Java基础环境
JAVA_HOME=`echo $JAVA_HOME`
if [ "$JAVA_HOME" != "" ]; then
    JAVA_VERSION=`$JAVA_HOME/bin/java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }'`
    IS_JDK8=`echo $JAVA_VERSION | grep "1.8"`
    if [ "$IS_JDK8" != "" ]; then
        JAVA="$JAVA_HOME/bin/java"
    fi
else
    JAVA_VERSION=`java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }'`
    if [ $? = 0 ]; then
        IS_JDK8=`echo $JAVA_VERSION | grep "1.8"`
        if [ "$IS_JDK8" == "" ]; then
            echo -e "\033[32m This env install JDK,but not JDK8,please install JDK8 \033[0m"
            exit 0
        else
            JAVA=`which java 2>&1 |awk 'NR==1{ gsub(/"/,""); print $0 }'`
        fi
    else
        echo "\033[32m This env not install JDK8,please install JDK8 \033[0m"
        exit 0
    fi
fi

# -server -Xms2048M -Xmx2048M -XX:MaxNewSize=512M -XX:PermSize=512M -XX:MaxPermSize=1024M -XX:+CMSParallelRemarkEnabled -XX:ParallelGCThreads=4 -Djava.awt.headless=true
JAVA_OPTS=""
# 获取shell脚本所在的绝对路径
SOURCE="$0"
while [ -h "$SOURCE"  ]; do
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE"
done

APP_BASE_PATH="$(cd -P "$( dirname "$SOURCE"  )" && pwd)"
# 设置jar绝对路径
APP_JAR=`find *.jar`
JAR_KEYWORDS="$APP_BASE_PATH/$APP_JAR"
# 设置程序名称
APP_NAME=$APP_JAR
# 查找服务进程号
PID=$(ps aux | grep ${JAR_KEYWORDS} | grep -v grep | awk '{print $2}' )

# 自动查找配置文件
APPLICATION_FILE=""
for file in conf/*; do
    if [[ $file =~ ^conf/application*.yml ]]; then
        APPLICATION_FILE=$file,$APPLICATION_FILE
    elif [[ $file =~ ^conf/application*.properties ]]; then
        APPLICATION_FILE=$file,$APPLICATION_FILE
    elif [[ $file =~ ^conf/bootstrap*.yml ]]; then
        APPLICATION_FILE=$file,$APPLICATION_FILE
    elif [[ $file =~ ^conf/bootstrap*.properties ]]; then
        APPLICATION_FILE=$file,$APPLICATION_FILE
    fi
done
# 删除最后多余的英文逗号
APPLICATION_FILE=${APPLICATION_FILE%,}

function check_if_process_is_running 
{
    if [ "$PID" = "" ]; then
        return 1
    fi
    ps -p $PID | grep "java"
    return $?
}

case "$1" in
    status)
        if check_if_process_is_running
        then
            echo -e "\033[32m $APP_NAME is running \033[0m"
        else
            echo -e "\033[32m $APP_NAME not running \033[0m"
        fi
        ;;
    stop)
        if ! check_if_process_is_running
        then
            echo -e "\033[32m $APP_NAME already stopped \033[0m"
            exit 0
        fi
        kill -9 $PID
        echo -e "\033[32m Waiting for process to stop \033[0m"
        NOT_KILLED=1
        for i in {1..20}; do
            if check_if_process_is_running
            then
                echo -ne "\033[32m . \033[0m"
                sleep 1
            else
                NOT_KILLED=0
            fi
        done
        if [ $NOT_KILLED = 1 ]
        then
            echo -e "\033[32m Cannot kill process \033[0m"
            exit 1
        fi
        echo -e "\033[32m $APP_NAME already stopped \033[0m"
        ;;
    start)
        if [ "$PID" != "" ] && check_if_process_is_running
        then
            echo -e "\033[32m $APP_NAME already running \033[0m"
            exit 1
        fi
        nohup $JAVA -jar $JAVA_OPTS $JAR_KEYWORDS --spring.config.location=$APPLICATION_FILE > /dev/null 2>&1 &
        #循环输出并提示
        tput sc
        n=1
        while :
        do
            if [ $n -eq 3 ]
            then
                tail -f $APP_BASE_PATH/logs/info.log
            fi
            if [ $n -le 120 ]
            then
                if check_if_process_is_running
                then
                    echo -e "\033[32m $APP_NAME started \033[0m"
                    exit 1
                else
                    echo -ne "\033[32m $APP_NAME Starting "$n"\033[0m"
                    sleep 1
                    n=$[$n+1]
                    tput rc
                    tput ed
                fi
            else
                echo -e "\033[32m $APP_NAME start failed \033[0m"
                exit 1
            fi
        done
        ;;
    restart)
        $0 stop
        if [ $? = 1 ]
        then
            exit 1
        fi
        $0 start
        ;;
    loginfo)
        tail -f $APP_BASE_PATH/logs/info.log
        exit 1
        ;;
    logerror)
        tail -f $APP_BASE_PATH/logs/error.log
        exit 1
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status|loginfo|logerror}"
        exit 1
esac

exit 0